package org.csu.myPetStore.web.servlet.Order;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Cart;
import org.csu.myPetStore.domain.Order;
import org.csu.myPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderDealServlet extends HttpServlet {

    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final SimpleDateFormat sdf = new SimpleDateFormat("MM-yyyy");

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        OrderService service = new OrderService();
        String s = req.getParameter("order.expiryDate");

        HttpSession session = req.getSession();
        Order order = (Order) session.getAttribute("order");

        Account account = (Account) session.getAttribute("account");
        Cart cart = (Cart) session.getAttribute("cart");


        if(order == null) {
            order = getOrder(req, resp,s);
            order.initOrder(account,cart);

            String e = req.getParameter("shippingAddressRequired");

            if(e!=null){
                order.setShipToFirstName(req.getParameter("order.shipToFirstName"));
                order.setShipToLastName(req.getParameter("order.shipToLastName"));
                order.setShipAddress1(req.getParameter("order.shipAddress1"));
                order.setShipAddress2(req.getParameter("order.shipAddress2"));
                order.setShipCity(req.getParameter("order.shipCity"));
                order.setShipState(req.getParameter("order.shipState"));
                order.setShipZip(req.getParameter("order.shipZip"));
                order.setShipCountry(req.getParameter("order.shipCountry"));
                session.setAttribute("order",order);
            }
            session.setAttribute("order", order);
        }else {
        }



        //跳转SHIPPER
        req.getRequestDispatcher(CONFIRM_ORDER).forward(req,resp);


    }

    protected Order getOrder(HttpServletRequest req, HttpServletResponse resp,String s) throws ServletException, IOException{
        Order order = new Order();

        HttpSession session = req.getSession();
        order.setCardType(req.getParameter("order.cardType"));
        order.setCreditCard(req.getParameter("order.creditCard"));
        order.setExpiryDate(req.getParameter("order.expiryDate"));
        order.setBillToFirstName(req.getParameter("order.billToFirstName"));
        order.setBillToLastName(req.getParameter("order.billToLastName"));
        order.setBillAddress1(req.getParameter("order.billAddress1"));
        order.setBillAddress2(req.getParameter("order.billAddress2"));
        order.setBillCity(req.getParameter("order.billCity"));
        order.setBillState(req.getParameter("order.billState"));
        order.setBillZip(req.getParameter("order.billZip"));
        order.setBillCountry(req.getParameter("order.billCountry"));
        order.setCourier("YunDa");
        order.setStatus("OK");
        order.setLocale("China");

        return order;
    }

}
